package net.csdn.www;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

public class UrlUtil {

	public static List<String> getHttpUrl(String urlStr,String regex) {
		List<String> list = new ArrayList<String>();
		URL url = null;
		URLConnection conn = null;
		String nextLine = null;
		StringTokenizer tokenizer = null;
		try {
			// 获得网页资源
			url = new URL(urlStr);
			// 获得资源连接
			conn = url.openConnection();
			conn.connect();
			BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
			// 开始读取网页信息解析出网页中的超链接
			while ((nextLine = reader.readLine()) != null) {
				tokenizer = new StringTokenizer(nextLine);
				while (tokenizer.hasMoreTokens()) {
					String urlToken = tokenizer.nextToken();
					if (RegexMatcher.isMatcher(regex, urlToken)) {
						list.add(getHttp(urlToken));
					}
				}
			}

		} catch (Exception e) {
			System.out.println("==[ hava error ]==" + e);
			e.printStackTrace();
		}
		return list;
	}
	
	public static List<String> getImgs(String urlStr,String[] imageRegexs) {
		List<String> list = new ArrayList<String>();
		URL url = null;
		URLConnection conn = null;
		String nextLine = null;
		StringTokenizer tokenizer = null;
		try {
			// 获得网页资源
			url = new URL(urlStr);
			// 获得资源连接
			conn = url.openConnection();
			conn.connect();
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					conn.getInputStream()));
			// 开始读取网页信息解析出网页中的超链接
			boolean flag = false;
			while ((nextLine = reader.readLine()) != null) {
				tokenizer = new StringTokenizer(nextLine);
				while (tokenizer.hasMoreTokens()) {
					String urlToken = tokenizer.nextToken();
					for (int i = 0, size = imageRegexs.length; i < size; i++) {
						if (RegexMatcher.isMatcher(imageRegexs[i], urlToken)) {
							flag = true;
							break;
						}
					}

					if (flag) {
						flag = false;
						String imgUrlStr = getWebPageImgs(urlToken);
						if (imgUrlStr != null) {
							list.add(imgUrlStr);
						}
					}
				}
			}

		} catch (Exception e) {
			System.out.println("==[ hava error ]==" + e);
			e.printStackTrace();
		}
		return list;
	}
	
	private static String getHttp(String urlToken) {
		int start = urlToken.indexOf("http");
		int end = urlToken.length();
		String tempStr = urlToken.substring(start, end);
		end = tempStr.indexOf("\"");
		if (end == -1) {
			end = tempStr.length();
		}
		return tempStr.substring(0, end);
	}
	
	private static String getWebPageImgs(String urlToken) {
		int start = urlToken.indexOf("\"");
		int end = urlToken.length() - 1;
		String tempStr = urlToken.substring(start + 1, end);
		if (urlToken.indexOf(".jpg") == -1) {
			tempStr += ".jpg";
		}
		return tempStr;
	}

}
